﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using BLL;
using Model;
using System.Text.RegularExpressions;

public partial class BaseData_UserMessageEdit : System.Web.UI.Page
{
    public int messageId
    {
        get
        {
            if (Request.QueryString["MessageId"] != null)
            {
                return Convert.ToInt32(Request.QueryString["MessageId"]);
            }
            return -1;
        }
    }
    public string productName
    {
        get
        {
            if (Request.QueryString["productName"] != null)
            {
                return Request.QueryString["productName"].Replace("?"," ");
            }
            return "";
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (messageId != -1)
            {
                UserMessage us;
                string ret = new BLL.UserMessageBL().getUserMessageByMessageId(messageId.ToString(),out us);
                if (ret == "000000")
                {
                    this.goodsId.Text = productName;
                    this.txtMessage.Text = us.MessageContent;
                    this.txtBackContent.Text = us.BackContent;
                    this.txtJF.Text = us.AddJF.ToString();
                }
            }
 
        }

    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (txtBackContent.Text.Trim() == string.Empty)
        {
            SiteCommon.Common.alert("回复内容不能为空");
            txtBackContent.Focus();
            return;
        }
        if (txtJF.Text.Trim() == string.Empty)
        {
            SiteCommon.Common.alert("积分奖罚不能为空");
            txtJF.Focus();
            return;
        }
        if (!CheckNum(txtJF.Text.Trim()))
        {
            SiteCommon.Common.alert("积分奖罚必须是浮点数");
            txtJF.Focus();
            return;
        }

        if (messageId != -1)
        {
            UserMessage us;
            string ret = new BLL.UserMessageBL().getUserMessageByMessageId(messageId.ToString(), out us);
            if (ret == "000000")
            {
                Model.V_VIPSET vipset = new BLL.V_VIPSET().GetModel(us.UserId);
                string sql = new BLL.UserMessageBL().UserMessageEdit(messageId, txtBackContent.Text, Share.UserValue(0), DateTime.Now, decimal.Parse(txtJF.Text.Trim()));

                sql = sql + ";insert into IntegralWasteBook(IntergalId,IntergalType,IntergalName,IntergalRange,IntergalDateTime,IntergalExtendAttribute1,IntergalExtendAttribute3,IntergalExtendAttribute7)values('" + Guid.NewGuid().ToString() + "','" + Convert.ToString((int)BLL.InergalManage.InergalTypeEnum.留言积分) + "','" + messageId.ToString() + "'," + decimal.Parse(txtJF.Text.Trim()) + ",'" + DateTime.Now.ToString() + "','" + us.UserId + "','" + BLL.InergalManage.InergalTypeEnum.留言积分.ToString() + "'," + vipset.DQJF + ")";
                int result = 0;
                string key = ConfigurationManager.AppSettings["CODE"];
                LX_Manage.ERP.Service service = new LX_Manage.ERP.Service();
                if (sql != "")
                {
                    result = service.UpdateCustomerDQJF(key, us.UserId, decimal.Parse(txtJF.Text.Trim()), sql);
                }
                if (result == 0)
                {
                    SiteCommon.Common.alert("操作失败");
                }
                else
                {
                    SiteCommon.Common.alert("回复成功", "UserMessageManagement.aspx");
                }

            }
        }
    }
    private bool CheckNum(string str)
    {
        Regex reg = new Regex(@"^(-?\d+)(\.\d+)?$");
        if(reg.IsMatch(str))
        {
            return true;
        }
        return false;
    }
    protected void btnReturn_Click(object sender, EventArgs e)
    {
        Response.Redirect("UserMessageManagement.aspx");
    }
}
